﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DemoforGit
{
    /// <summary>
    /// 棋子
    /// </summary>
    class Chess
    {
        public Rectangle Rectangle { get; set; }        
        private const int Step = 20;
        private int SpeedX;
        private int SpeedY;


        public Chess(Rectangle rec)
        {
            Rectangle = rec;
            SpeedX = 0;
            SpeedY = 8;

        }
        public void Draw(Graphics g)
        {

            g.DrawImage(Properties.Resources.chess, Rectangle);
        }
        /// <summary>
        /// 处理键盘
        /// </summary>
        /// <param name="Key"></param>
        public void KeyDown(String Key)
        {
            int newX = Rectangle.X;
            if (Key == "A")
            {
                newX += Step;
                //防止右出界
                if (newX > 1000)
                    newX = 1000;
            }
            Rectangle = new Rectangle(newX,
                                       Rectangle.Y,
                                       Rectangle.Width,
                                       Rectangle.Height);

            if (Key == "D")
            {
                newX -= Step;
                //防止左出界
                if (newX < 0)
                    newX = 0;
            }
            Rectangle = new Rectangle(newX,
                                       Rectangle.Y,
                                       Rectangle.Width,
                                       Rectangle.Height);

            int newY = Rectangle.Y;
            if (Key == "D")
            {
                newY += Step;
                //防止下出界
                if (newY >700)
                    newY = 700;
            }
            Rectangle = new Rectangle(Rectangle.X,
                                       newY,
                                       Rectangle.Width,
                                       Rectangle.Height);
            if (Key == "A")
            {
                newY -= Step;
                //防止上出界
                if (newY < 0)
                    newY = 0;
            }
            Rectangle = new Rectangle(Rectangle.X,
                                       newY,
                                       Rectangle.Width,
                                       Rectangle.Height);
        }



        public void Move(Size size)
        {
            this.Rectangle = new Rectangle(Rectangle.X + SpeedX,
                 Rectangle.Y + SpeedY, Rectangle.Width, Rectangle.Height);

            if (this.Rectangle.X<0)
            {
                this.Rectangle = new Rectangle(0,
                    this.Rectangle.Y, Rectangle.Width, Rectangle.Height);
                SpeedX *= 1;
            }

            if (this.Rectangle.Y <0)
            {
                this.Rectangle = new Rectangle(this.Rectangle.X,
                    0, Rectangle.Width, Rectangle.Height);
                SpeedY *= -1;
            }


        }
        public void ChangeVerDiction()
        {
            SpeedY *= -1;
        }
       
    }

}
